package com.wcl.demo.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.ImageView;
import android.widget.Toast;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.core.PoiInfo;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.poi.OnGetPoiSearchResultListener;
import com.baidu.mapapi.search.poi.PoiDetailResult;
import com.baidu.mapapi.search.poi.PoiDetailSearchOption;
import com.baidu.mapapi.search.poi.PoiNearbySearchOption;
import com.baidu.mapapi.search.poi.PoiResult;
import com.baidu.mapapi.search.poi.PoiSearch;
import com.wcl.demo.R;
import com.wcl.demo.utils.overlayutil.PoiOverlay;
import com.wcl.demo.ui.activity.iteminterface.OnLocationItemClickListener;
import com.wcl.demo.ui.adapter.LocationAdapter;

import java.util.List;

/**
 * Created by dllo on 15/12/2.
 */
public class LocationActivity extends BaseActivity implements OnLocationItemClickListener {
    private MapView location_mapView;
    private BaiduMap map;//实现定位功能
    private LocationClient client;//实现LocationClient
    private RecyclerView recyclerView;
    private PoiSearch mPoiSearch = null;//定义检索
    private LocationAdapter adapter;//定义适配器
    private ImageView location_backIv;

    @Override
    protected int getLayout() {
        return R.layout.activity_lcation;
    }

    @Override
    protected void initView() {
        adapter = new LocationAdapter();
        recyclerView = (RecyclerView) findViewById(R.id.location_recyclerView);
        recyclerView.setAdapter(adapter);
        GridLayoutManager manager = new GridLayoutManager(this , 1);
        recyclerView.setLayoutManager(manager);
        adapter.setLocationClickListener(this);

        location_backIv = (ImageView) findViewById(R.id.location_backIv);


        mPoiSearch = PoiSearch.newInstance();//创建Poi检索实例
        OnGetPoiSearchResultListener listener = new OnGetPoiSearchResultListener() {
            @Override
            public void onGetPoiResult(PoiResult poiResult) {
                //获取POI检索结果
                if (poiResult == null|| poiResult.error == SearchResult.ERRORNO.RESULT_NOT_FOUND) {
                    Toast.makeText(LocationActivity.this, "没有这个位置,请重新检索", Toast.LENGTH_SHORT).show();
                    return;
                }
                Log.i("lanou4g", poiResult + "???????????????????");
                PoiOverlay overlay = new MyPoiOverlay(map);
                map.setOnMarkerClickListener(overlay);
                overlay.setData(poiResult);
                overlay.addToMap();
                overlay.zoomToSpan();
                List<PoiInfo> list=poiResult.getAllPoi();
                adapter.addData(list);
            }

            @Override
            public void onGetPoiDetailResult(PoiDetailResult poiDetailResult) {
                //获取Place详情页检索结果
            }
        };
        mPoiSearch.setOnGetPoiSearchResultListener(listener);

        recyclerView = (RecyclerView) findViewById(R.id.location_recyclerView);

        location_mapView = (MapView) findViewById(R.id.location_mapView);
        map = location_mapView.getMap();//得到Baidu地图
        map.setMyLocationEnabled(true);//开启定位功能
        client = new LocationClient(this);//初始化LocationClient定位客户端
        client.registerLocationListener(new MyLocationListenner());//注册监听回调对象

        LocationClientOption option = new LocationClientOption();
        option.setScanSpan(1000);//每隔1秒扫描定位一次
        option.setCoorType("bd09ll");//设置坐标类型
        option.setOpenGps(true);//设置是否开启GPS
        option.setAddrType("all");//获取地址信息类型
        option.setIsNeedAddress(true);//获取地址信息
        client.setLocOption(option);//将定位服务设置到客户端
        client.start();//开启定位
    }

    //行点击事件
    @Override
    public void onClickLocationListener(PoiInfo results) {
        Intent intent = new Intent(this , ChatActivity.class);
        Bundle bundle = new Bundle();
        bundle.putParcelable("sendLocation" , results);
        intent.putExtras(bundle);
        Log.i("android" , results.toString() + "-------------------------->>>>>>>>>>>>1");
        String username = getIntent().getStringExtra("username");
        intent.putExtra("username",username);
        setResult(10 ,intent);
        finish();
    }

    /**
     * 定位SDK监听函数
     */
    public class MyLocationListenner implements BDLocationListener {

        @Override
        public void onReceiveLocation(BDLocation location) {
            // map view 销毁后不在处理新接收的位置
            if (location == null || location_mapView == null) {
                return;
            }
            MyLocationData locData = new MyLocationData.Builder()
                    .accuracy(location.getRadius())
                            // 此处设置开发者获取到的方向信息，顺时针0-360
                    .direction(100).latitude(location.getLatitude())//纬度
                    .longitude(location.getLongitude()).build();//经度
            map.setMyLocationData(locData);
            LatLng ll = new LatLng(location.getLatitude(),
                    location.getLongitude());
            Log.i("sxxc", ll + "_________________");
            MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll);
            //周边检索
            map.animateMapStatus(u);
            PoiNearbySearchOption option = new PoiNearbySearchOption();
            option.keyword("餐厅");
            option.location(ll);
            option.radius(10000);
            mPoiSearch.searchNearby(option);
        }
    }
    @Override
    protected void initData() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        location_mapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        location_mapView.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        location_mapView.onDestroy();
        mPoiSearch.destroy();
        client.stop();
    }

    private class MyPoiOverlay extends PoiOverlay {

        public MyPoiOverlay(BaiduMap baiduMap) {
            super(baiduMap);
        }

        @Override
        public boolean onPoiClick(int index) {
            super.onPoiClick(index);
            PoiInfo poi = getPoiResult().getAllPoi().get(index);
            // if (poi.hasCaterDetails) {
            mPoiSearch.searchPoiDetail((new PoiDetailSearchOption())
                    .poiUid(poi.uid));
            // }
            return true;
        }
    }
}
